#include "InputsManager.h"
#include "CustomButton.h"

InputsManager::InputsManager()
{
	

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	Keyid[KEY_JUMP] = VK_SPACE;
	Keyid[KEY_LEFT] = VK_LEFT;
	Keyid[KEY_RIGHT] = VK_RIGHT;
	Keyid[KEY_RUN] = VK_CONTROL;
#endif

	memset( &KeyList, 0, sizeof(Key) * KEY_COUNT );
	memset( &VirtualKeyList, 0, sizeof(CustomButton*) * KEY_COUNT );
};


InputsManager* InputsManager::GetInstance()
{
	static InputsManager manager;

	return &manager;
}

void InputsManager::SetVirtualKey( int Key_Id, CustomButton* _virtualKey )
{
	VirtualKeyList[Key_Id] = _virtualKey;
}

void InputsManager::SetKeyPress( int Key_Id, bool isPress )
{
    if( isPress && KeyList[Key_Id].Press == false )
    {
        KeyList[Key_Id].Pressed = true;
        KeyList[Key_Id].FrameSameState = 0;
    }
    else if(  isPress == false &&  KeyList[Key_Id].Press == true )
    {
        KeyList[Key_Id].Released = true;
        KeyList[Key_Id].FrameSameState = 0;
    }
    
    KeyList[Key_Id].Press = isPress;
}

void InputsManager::DeleteVirtualKey( )
{
	//CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this, 0, false );
	//CCTouchDispatcher::sharedDispatcher()->addStandardDelegate( this, 0 );

	CCTouchDispatcher::sharedDispatcher()->removeDelegate( this );

	VirtualKeyList[KEY_LEFT]->release();
	VirtualKeyList[KEY_LEFT] = NULL;

	VirtualKeyList[KEY_RIGHT]->release();
	VirtualKeyList[KEY_RIGHT] = NULL;

	VirtualKeyList[KEY_JUMP]->release();
	VirtualKeyList[KEY_JUMP] = NULL;

	VirtualKeyList[KEY_RUN]->release();
	VirtualKeyList[KEY_RUN] = NULL;
}

void InputsManager::CreateVirtualKey( CCNode* Parent )
{
	CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this, 0, false );
	//CCTouchDispatcher::sharedDispatcher()->addStandardDelegate( this, 0 );

	VirtualKeyList[KEY_LEFT] = CustomButton::ButtonWithNormalImage("CloseNormal.png","CloseSelected.png");
	VirtualKeyList[KEY_LEFT]->setPosition( ccp( 40, 40) );
    VirtualKeyList[KEY_LEFT]->setScale(2.0f);
	VirtualKeyList[KEY_LEFT]->ComputeBoundingTouch();
	VirtualKeyList[KEY_LEFT]->SetBoundingTouch( CCRect( 0.0f, 0.0f, 80.0f, 80.0f) );
	Parent->addChild( VirtualKeyList[KEY_LEFT], 1 );
    
	VirtualKeyList[KEY_RIGHT] =  CustomButton::ButtonWithNormalImage("CloseNormal.png","CloseSelected.png" );
	VirtualKeyList[KEY_RIGHT]->setPosition( ccp( 80 + 40, 40) );
    VirtualKeyList[KEY_RIGHT]->setScale(2.0f);
	VirtualKeyList[KEY_RIGHT]->ComputeBoundingTouch();
	VirtualKeyList[KEY_RIGHT]->SetBoundingTouch( CCRect( 80.0f, 0.0f, 80.0f, 80.0f) );
    Parent->addChild( VirtualKeyList[KEY_RIGHT], 1 );
    
    VirtualKeyList[KEY_JUMP] =  CustomButton::ButtonWithNormalImage("CloseNormal.png","CloseSelected.png");
	VirtualKeyList[KEY_JUMP]->setPosition( ccp( CCDirector::sharedDirector()->getWinSize().width - 40, 40) );
    VirtualKeyList[KEY_JUMP]->setScale(2.0f);
	VirtualKeyList[KEY_JUMP]->ComputeBoundingTouch();
	VirtualKeyList[KEY_JUMP]->SetTouchbyArea( true );
	VirtualKeyList[KEY_JUMP]->SetBoundingTouch( CCRect( 400.0f, 0.0f, 80.0f, 80.0f) );
    Parent->addChild( VirtualKeyList[KEY_JUMP], 1 );
    
	VirtualKeyList[KEY_RUN] =  CustomButton::ButtonWithNormalImage("CloseNormal.png", "CloseSelected.png" );
	VirtualKeyList[KEY_RUN]->setPosition( ccp( CCDirector::sharedDirector()->getWinSize().width - 40 - 80, 40) );
	VirtualKeyList[KEY_RUN]->setScale(2.0f);
	VirtualKeyList[KEY_RUN]->ComputeBoundingTouch();
	VirtualKeyList[KEY_RUN]->SetTouchbyArea( true );
	VirtualKeyList[KEY_RUN]->SetBoundingTouch( CCRect( 400.0f - 80.0f, 0.0f, 80.0f, 80.0f) );
	Parent->addChild(  VirtualKeyList[KEY_RUN], 1 );
}


void InputsManager::update(ccTime dt)
{
	for( int i=0; i<KEY_COUNT; i++ )
	{
		KeyList[i].FrameSameState++;

		KeyList[i].Pressed = false;
		KeyList[i].Released = false;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		if( GetAsyncKeyState( Keyid[i] ) && KeyList[i].Press == false )
		{
			KeyList[i].Pressed = true;
			KeyList[i].FrameSameState = 0;
		}
		else if(  GetAsyncKeyState( Keyid[i] ) == false && KeyList[i].Press == true )
		{
			KeyList[i].Released = true;
			KeyList[i].FrameSameState = 0;
		}
		KeyList[i].Press = GetAsyncKeyState( Keyid[i] ) ? true : false;
#else 
		if( VirtualKeyList[i] )
		{
			if( VirtualKeyList[i]->isSelected() && KeyList[i].Press == false )
			{
				KeyList[i].Pressed = true;
				KeyList[i].FrameSameState = 0;
			}
			else if(  VirtualKeyList[i]->isSelected() == false && KeyList[i].Press == true )
			{
				KeyList[i].Released = true;
				KeyList[i].FrameSameState = 0;
			}
			KeyList[i].Press = VirtualKeyList[i]->isSelected();
		}
#endif
	}
}


void InputsManager::draw()
{
	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id != NULL )
		{
			CCPoint lowerBound = ListTouchArea[i].Start;
			CCPoint upperBound = ListTouchArea[i].End;;


			ccDrawLine( CCPoint(lowerBound.x  , lowerBound.y  ) ,
						CCPoint(upperBound.x  , lowerBound.y  )  );

			ccDrawLine( CCPoint(upperBound.x , lowerBound.y  ) ,
						CCPoint(upperBound.x , upperBound.y  )  );

			ccDrawLine( CCPoint(upperBound.x , upperBound.y  ) ,
						CCPoint(lowerBound.x , upperBound.y  )  );

			ccDrawLine( CCPoint(lowerBound.x , upperBound.y  ) ,
						CCPoint(lowerBound.x , lowerBound.y  )  );
		}
	}

	for( int i=0; i<KEY_COUNT; i++ )
	{
		if( VirtualKeyList[i] )
		{
			VirtualKeyList[i]->drawBoundingBox();
		}
	}
}


void InputsManager::RefreshVirtualKey()
{
	for( int j=0; j < KEY_COUNT; j++ )
	{
		if( VirtualKeyList[j] )
		{
			VirtualKeyList[j]->SetisSelected( false );
		}
	}

	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id )
		{
			for( int j=0; j < KEY_COUNT; j++ )
			{
				if( VirtualKeyList[j] )
				{
					if( VirtualKeyList[j]->isTouchByArea() )
					{
						CCRect TouchRect = CCRect( );/* ListTouchArea[i].Start.x,  ListTouchArea[i].Start.y,
													ListTouchArea[i].End.x - ListTouchArea[i].Start.x, ListTouchArea[i].End.y - ListTouchArea[i].Start.y ); */
						TouchRect.origin = CCPoint( MIN( ListTouchArea[i].Start.x, ListTouchArea[i].End.x ), MIN( ListTouchArea[i].Start.y, ListTouchArea[i].End.y ) );
						TouchRect.size = CCSize( abs( ListTouchArea[i].End.x -  ListTouchArea[i].Start.x ), abs( ListTouchArea[i].End.y -  ListTouchArea[i].Start.y ) );

						if( CCRect::CCRectIntersectsRect(  VirtualKeyList[j]->GetBoundingTouch() , TouchRect ) )
						{
							VirtualKeyList[j]->SetisSelected( true );
						}
					}
					else
					{
						if( CCRect::CCRectContainsPoint( VirtualKeyList[j]->GetBoundingTouch(), ListTouchArea[i].End ) )
						{
							VirtualKeyList[j]->SetisSelected( true );
						}
					}
				}
			} 
		}
	}
}

// delegate 

bool InputsManager::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	CCPoint touchLocation = touch->locationInView(touch->view());
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
    
    printf( "Who : %d          X : %f      Y : %f\n",touch, touchLocation.x, touchLocation.y );

	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id == NULL )
		{
			ListTouchArea[i] = TouchArea( touch,  touchLocation );
			break;
		}
	}

	RefreshVirtualKey();

	return true;
}
void InputsManager::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id == touch )
		{
			ListTouchArea[i] = TouchArea();
		}
	}

	RefreshVirtualKey();
}
void InputsManager::ccTouchCancelled(CCTouch *touch, CCEvent* event)
{
	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id == touch )
		{
			ListTouchArea[i] = TouchArea();
		}
	}

	RefreshVirtualKey();
}
void InputsManager::ccTouchMoved(CCTouch* touch, CCEvent* event)
{ 
	CCPoint touchLocation = touch->locationInView(touch->view());
	touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);

	for(int i=0; i< MAX_TOUCH_AREA; i++ )
	{
		if( ListTouchArea[i].Id == touch )
		{
			ListTouchArea[i].End = touchLocation;
		}
	}

	RefreshVirtualKey();
}





void InputsManager::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
};
void InputsManager::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
};
void InputsManager::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
};
void InputsManager::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
};